package com.example.navbardemo.slice;

import com.example.navbardemo.Entity.BottomNavItem;
import com.example.navbardemo.ResourceTable;
import com.example.navbardemo.fraction.CompontentPage;
import com.example.navbardemo.fraction.SystemPage;
import com.example.navbardemo.model.BottomNavViewModel;
import com.example.navbardemo.utils.FractionUtil;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    FractionUtil fractionUtil = new FractionUtil();
    List<Fraction> fractions = new ArrayList<>();
    BottomNavViewModel bottomNavViewModel = new BottomNavViewModel();
    @Override
    public void onStart(Intent intent) {

        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        fractions =  bottomNavViewModel.initBottomNavFractions();
        initNavTopBar();
//        initmainBodyContent();
        initBottom();
//            this.getFractionManager
        // 获取Fraction管理器
       try {
           ((FractionAbility) getAbility()).getFractionManager()
                   .startFractionScheduler()
                   .add(ResourceTable.Id_main_content,fractions.get(0)  )
                   .submit();

       }catch (Exception e){
           e.printStackTrace();
       }

    }
    private static FractionManager getFractionManager(AbilitySlice abilitySlice){
        Ability ability = abilitySlice.getAbility();
        if(ability instanceof FractionAbility){
            FractionAbility fractionAbility = (FractionAbility) ability;
            return fractionAbility.getFractionManager();
        }
        return null;
    }
    @Override
    public void onActive() {
        super.onActive();

    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
    private void initNavTopBar(){
        //获取主页面的内容容器id
        DirectionalLayout NavTopBaCompontentr =(DirectionalLayout) findComponentById(ResourceTable.Id_nav_top_bar);
        Component parse = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_nav_top_bar, null, false);
        NavTopBaCompontentr.addComponent(parse);
    }
    private void initmainBodyContent(){
        /*
        *
        * Ability是应用所具备能力的抽象，也是应用程序的重要组成部分。一个应用可以具备多种能力（即可以包含多个Ability），
        * HarmonyOS支持应用以Ability为单位进行部署。Ability可以分为FA（Feature Ability）和PA（Particle Ability）两种类型，
        * 每种类型为开发者提供了不同的模板，以便实现不同的业务功能。
        * 解析 有错误可以提出来
        * 获取当前的ability 转成 FractionAbility  getFractionManager 获取操作权限
        * */

//            new FractionAbility();


    }

    /**
     * 自定义底部点击事件
     * @param item   当前组件
     * @param tabList  底部组件列表
     * @param index  当前索引值
     */
    private void bottomNavBarClick(DirectionalLayout item,List<DirectionalLayout> tabList,int index){

        item.setClickedListener(component -> {
            // Deselect all tabs in tab menu
            int i=0;
            for (DirectionalLayout btn : tabList) {
                Image componentById = (Image) btn.findComponentById(ResourceTable.Id_bottom_tab_button_img);
                componentById.setPixelMap(ResourceTable.Media_icon_normal);
                i++;
            }
            Image componentById =(Image) component.findComponentById(ResourceTable.Id_bottom_tab_button_img);
            componentById.setPixelMap(ResourceTable.Media_icon_actived);
            if(index==0){
                try {
                    ((FractionAbility)getAbility()).getFractionManager()
                            .startFractionScheduler()
                            .show(fractions.get(0))
                            .submit();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            if(index==1){
                System.out.println("1111111111111111111");
                System.out.println(fractions.get(0)==fractions.get(0));
                ((FractionAbility)getAbility()).getFractionManager()
                        .startFractionScheduler()
                        .hide(fractions.get(0))
                        .submit();
            }
        });

    }

    /**
     * 初始化底部导航
     */
    private void  initBottom(){
        ArrayList<BottomNavItem> textinfos = new ArrayList<>();
        textinfos.add(new BottomNavItem("布局组件"));
        textinfos.add(new BottomNavItem("系统环境"));
        //获取主页面底部容器ID
        DirectionalLayout bottomTab = (DirectionalLayout) findComponentById(ResourceTable.Id_bottom_tabMenu);
        //创建底部容器数据数组
        List<DirectionalLayout> tabList = new ArrayList<>();
        for (int count = 0; count < 2; count++) {
            //获取模板容器
            DirectionalLayout tab = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                    .parse(ResourceTable.Layout_bottom_nav_tab, bottomTab, false);
            //获取模板容器组件
            Image buttonImage = (Image) tab.findComponentById(ResourceTable.Id_bottom_tab_button_img);
            Text buttonText = (Text) tab.findComponentById(ResourceTable.Id_bottom_tab_button_text);
            if (buttonImage != null) {//
                if (count == 0) {
                    buttonImage.setPixelMap(ResourceTable.Media_icon_actived);
                } else {
                    buttonImage.setPixelMap(ResourceTable.Media_icon_normal);
                }
            }
            if (buttonText != null) {
                buttonText.setText(textinfos.get(count).getAuthor());
            }
//            //绑定点击事件
            bottomNavBarClick(tab,tabList,count);
            tabList.add(tab);
            bottomTab.addComponent(tab);
        }

    }
}
